//
//  HeadImageSizeView.m
//  FlowerRainGround
//
//  Created by 范明星 on 16/4/23.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "HeadImageSizeView.h"

static CGRect oldframe;

@implementation HeadImageSizeView


//点击图片放大
//传入一个ImageView
+(void)showImage:(UIImageView *)avatarImageView{
        UIImage *image= avatarImageView.image;
        UIWindow *window=[UIApplication sharedApplication].keyWindow;
    
     //在图片放大后要在图片下面加上一个view,frame为全屏
        UIView *backgroundView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height)];
    
    //头像所在的imageview的frame，定了个全局变量，注意rect用bounds
        oldframe=[avatarImageView convertRect:avatarImageView.bounds toView:window];
        backgroundView.backgroundColor=[UIColor blackColor];
        backgroundView.alpha=0;
    
    //创建一个imageView图片是点击imageview的image,frame是也是它的
        UIImageView *imageView=[[UIImageView alloc]initWithFrame:oldframe];
        imageView.image=image;
        imageView.tag=1;//标记这个imageview
        [backgroundView addSubview:imageView];
        [window addSubview:backgroundView];

    //再次轻拍这imageview执行
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(hideImage:)];
         [backgroundView addGestureRecognizer: tap];

           [UIView animateWithDuration:0.3 animations:^{
              imageView.frame=CGRectMake(0,([UIScreen mainScreen].bounds.size.height-image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width)/2, [UIScreen mainScreen].bounds.size.width, image.size.height*[UIScreen mainScreen].bounds.size.width/image.size.width);
               backgroundView.alpha=1;
           } completion:^(BOOL finished) {
    
        }];
}

+(void)hideImage:(UITapGestureRecognizer*)tap{
    UIView *backgroundView=tap.view;
    UIImageView *imageView=(UIImageView*)[tap.view viewWithTag:1];
    [UIView animateWithDuration:0.3 animations:^{
        imageView.frame=oldframe;
        backgroundView.alpha=0;
    } completion:^(BOOL finished) {
        [backgroundView removeFromSuperview];
    }];
}


@end
